package ca.etsmtl.log430.lab3;

/**
 * This class reads course descriptions from the input file and instantiates the
 * Course objects in the system.
 * 
 * Each line of input is read and expected in the following format
 * 
 * <pre>
 * 	LOG420 1 LME 1030 1200 Champagne  Architectures et conception de logiciels
 * 	|      | |   |    |    |          |
 * 	|      | |   |    |    Instructor Course Title
 * 	|      | |   |    Stop Time
 * 	|      | |   Start Time
 * 	|      | Days
 * 	|      Group
 * 	Course Number
 * </pre>
 * 
 * The coursLOG.txt file is provided as an example.
 * 
 * @author A.J. Lattanze, CMU
 * @version 1.2, 2011-Feb-24
 */

/*
 * Modification Log **********************************************************
 * 
 * v1.2, R. Champagne, 2011-Feb-24 - Various refactorings, conversion of
 * comments to javadoc format.
 * 
 * v1.1, R. Champagne, 2002-Jun-19 - Adapted for use at ETS.
 * 
 * v1.0, A.J. Lattanze, 12/29/99 - Original version.
 * ***************************************************************************
 */

public class CourseReader extends LineOfTextFileReader {
	private CourseList listOfCourses = new CourseList();

	public CourseReader() {
		listOfCourses = null;
	}

	public CourseReader(String inputFile) {
		listOfCourses = GetCourseList(inputFile);
	}

	/**
	 * Parses lines of text that are read from the text file containing course
	 * information.
	 * 
	 * @param lineOfText
	 * @return
	 */
	private Course parseCourseText(String lineOfText) {

		boolean done; // Loop terminator
		String token; // String token parsed from LineOfText
		int tokenCount; // Number of tokens parsed
		int frontIndex; // Front index of token to parse
		int backIndex; // Back index of token to parse

		// Instantiate a course object
		Course newCourse = new Course();

		tokenCount = 0;
		frontIndex = 0;
		backIndex = 0;
		done = false;

		while (!done) {
			backIndex = lineOfText.indexOf(' ', frontIndex);

			if (tokenCount < 6) {
				token = lineOfText.substring(frontIndex, backIndex);
			} else {
				token = lineOfText.substring(frontIndex);
			}

			switch (tokenCount) {
			case 0: // Course number
				newCourse.setCourseID(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;
			case 1: // Course Section ID
				newCourse.setSection(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;
			case 2: // Days of the week the course will be held on
				newCourse.setDays(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;
			case 3: // Course start time
				newCourse.setStartTime(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;
			case 4: // Course stop time
				newCourse.setStopTime(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;
			case 5: // Course instructor
				newCourse.setInstructor(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;
			case 6: // Course title or name of the course
				newCourse.setCourseName(token);
				done = true;
				break;
			}
		}
		return (newCourse);
	}

	/**
	 * Reads a line of text from the course file. The line of text is then
	 * passed to the parseCourseText method where it is parsed and a Course
	 * object is returned. The Course object is then added to the list. When a
	 * null is read from the Course file the method terminates and returns the
	 * list to the caller.
	 * 
	 * @param inputFile
	 * @return A list of Courses, null if empty.
	 */
	public CourseList GetCourseList(String inputFile) {

		String courseText;
		boolean done;
		CourseList listObject = new CourseList();

		if (openFile(inputFile)) {
			done = false;

			while (!done) {
				try {
					courseText = readLineOfText();
					if (courseText == null) {
						done = true;
					} else {
						listObject.addCourse(parseCourseText(courseText));
					}
				} catch (Exception err) {
					return (null);
				}
			}
		} else {
			return (null);
		}
		closeFile();
		return (listObject);
	}

	/**
	 * @return The list of courses.
	 */
	public CourseList getListOfCourses() {
		return listOfCourses;
	}

}
